<template>
  <div>
    <simple-data-dialog
      :title="title"
      :visible.sync="dialogVisible"
    >
      <el-form
        ref="info"
        :model="info"
        :rules="rules"
        :inline="true"
      >
        <el-form-item
          :label="$t('ims.supplier.supplyInfoName')"
          prop="supplyInfoName"
          required
        >
          <el-input
            v-model="info.supplyInfoName"
            :placeholder="$t('common.enter')+$t('ims.supplier.supplyInfoName')"
            :disabled="isAdd?false:true"
          />
        </el-form-item>
        <el-form-item
          :label="$t('ims.supplier.groupName')"
          prop="groupName"
          required
        >
          <el-input
            v-model="info.groupName"
            :placeholder="$t('common.enter')+$t('ims.supplier.groupName')"
          />
        </el-form-item>

        <el-form-item
          :label="$t('ims.supplier.groupDescribe')"
          prop="groupDescribe"
          label-width="100%"
        >
          <el-input
            v-model="info.groupDescribe"
            type="textarea"
            rows="3"
            :placeholder="$t('common.enter')+$t('ims.supplier.groupDescribe')"
          />
        </el-form-item>
      </el-form>
      <div
        slot="footer"
        class="dialog_btn"
      >
        <el-button @click="dialogVisible=false">{{ $t('common.cancel') }}</el-button>
        <!-- <el-button @click="reset('dialog')">{{$t('common.reset')}}</el-button> -->
        <el-button
          type="primary"
          @click="submitForm"
        >{{ $t('common.ok') }}
        </el-button>
      </div>
    </simple-data-dialog>
  </div>
</template>

<script>
import imsApi from '@/api/ims/ims'
import SimpleDataDialog from '@/components/SimpleDataDialog'

export default {
  name: '',
  components: { SimpleDataDialog },
  data() {
    this.rules = {
      supplyInfoName: [
        {
          required: true,
          message: this.$t('common.select') + this.$t('ims.supplier.supplyInfoName'),
          trigger: ['blur', 'change']
        }
      ],
      groupName: [
        {
          required: true,
          message: this.$t('common.enter') + this.$t('ims.supplier.groupName'),
          trigger: ['blur', 'change']
        }
      ]
    }
    return {
      dialogVisible: false,
      title: '',
      isAdd: '',
      applicationList: [],
      menuGroupList: [],
      oldDialogColumn: [],
      dialogColumn: [
        {
          paramName: null,
          paramKey: null,
          paramValue: null
        }
      ],
      info: {
        applicationCode: '',
        productName: '',
        version: '',
        status: '0',
        productMenuGroup: [],
        productMenuGroupVOList: [],
        productParamVOList: []
      }
    }
  },
  watch: {
    dialogVisible(val) {
      if (val) {
        this.$nextTick(() => {
          this.$refs.info && this.$refs.info.clearValidate()
        })
      }
    }
  },

  methods: {
    // 提交信息
    submitForm() {
      const infoRef = new Promise((resolve, reject) => {
        this.$refs.info.validate((valid) => {
          if (valid) resolve()
        })
      })
      const refList = [infoRef]
      Promise.all(refList).then(async() => {
        console.log(this.info)
        if (this.isAdd) {
          imsApi.addSupplyMerchandiseGroup(this.info).then(res => {
            this.$message.success(this.$t('common.addSuccess'))
            this.dialogVisible = false
            this.$emit('resetList')
          })
        } else {
          imsApi.updateSupplyMerchandiseGroup(this.info).then(res => {
            this.$message.success(this.$t('common.editSuccess'))
            this.dialogVisible = false
            this.$emit('resetList')
          })
        }
      })
    },
    // 重置
    reset(type) {
      // eslint-disable-next-line eqeqeq
      if (type == 'search') {
        for (const key in this.search) {
          const val = this.search[key]
          // eslint-disable-next-line eqeqeq
          if (val.constructor == Object) {
            this.search[key].value = null
          }
        }
        this.pagination.pages = 1
        this.searchData = {}
        this.getListBySearch()
        // eslint-disable-next-line eqeqeq
      } else if (type == 'dialog') {
        this.$refs['info'].resetFields()
        this.dialogColumn.map((item, index) => {
          this.$refs['codeInfo_' + index].resetFields()
          this.$refs['typeInfo_' + index].resetFields()
        })
        if (this.isAdd) {
          this.dialogColumn = [
            {
              posTradeCode: null,
              posTradeType: null
            }
          ]
        } else {
          this.dialogColumn = JSON.parse(JSON.stringify(this.oldDialogColumn))
        }
      }
    },
    del(index) {
      if (this.dialogColumn.length > 1) {
        this.dialogColumn.splice(index, 1)
      }
    }
  }
}
</script>

<style lang="scss" scoped>
</style>

